package order

import (
	"context"
	"fmt"
	"group-buy/group_buy/internal/models"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CompanyCapitalRecordLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCompanyCapitalRecordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CompanyCapitalRecordLogic {
	return &CompanyCapitalRecordLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CompanyCapitalRecordLogic) CompanyCapitalRecord(req *types.CompanyCapitalRecordReq) (resp *types.CapitalRecordRsp, err error) {
	data := make([]*models.OrderDetail, 0)
	//count, err := l.svcCtx.Engine.Where("status>1").FindAndCount(&data)
	// sql := l.svcCtx.Engine.Where("status>1")
	sql := l.svcCtx.Engine.Table("order")
	sql = sql.Join("LEFT", "member as dealer", "dealer.id=order.dealer_id")
	sql = sql.Join("LEFT", "member as invite_user", "invite_user.id=order.invite_id")
	sql = sql.Join("LEFT", "business as business", "business.id=order.business_id")
	sql = sql.Join("LEFT", "member as business_user", "business_user.id=business.user_id")
	sql = sql.Where("status>1")
	// listSql := l.svcCtx.Engine.Where("status>1")
	listSql := l.svcCtx.Engine.Table("order")
	listSql = listSql.Join("LEFT", "member as dealer", "dealer.id=order.dealer_id")
	listSql = listSql.Join("LEFT", "member as invite_user", "order.invite_id=invite_user.id")
	listSql = listSql.Join("LEFT", "business as business", "business.id=order.business_id")
	listSql = listSql.Join("LEFT", "member as business_user", "business_user.id=business.user_id")
	listSql = listSql.Where("status>1")
	if req.BusinessId > 0 {
		sql = sql.And("order.business_id=?", req.BusinessId)
		listSql = listSql.And("order.business_id=?", req.BusinessId)
	}
	if req.DealerId > 0 {
		sql = sql.And("order.dealer_id=?", req.DealerId)
		listSql = listSql.And("order.dealer_id=?", req.DealerId)
	}
	if req.ProductId > 0 {
		sql = sql.And("order.product_id=?", req.ProductId)
		listSql = listSql.And("order.product_id=?", req.ProductId)
	}
	if len(req.Date) > 0 {
		sql = sql.And("order.pay_time like ?", req.Date+"%")
		listSql = listSql.And("order.pay_time like ?", req.Date+"%")
	}
	amount, err := sql.Sum(&models.Order{}, "company_amount")
	if err != nil {
		return nil, err
	}
	count, err := listSql.Desc("order.id").Limit(req.Size, (req.Page-1)*req.Size).FindAndCount(&data)

	if err != nil {
		return nil, err
	}
	list := make([]*types.OrderDivision, len(data), len(data))
	for i, _ := range data {
		list[i] = &types.OrderDivision{
			// Id:          0,
			// OrderId:     data[i].Id,
			// OrderCode:   data[i].Code,
			// DivUserId:   0,
			// DivAmount:   data[i].CompanyAmount,
			// DivTime:     data[i].PayTime,
			// ProductId:   data[i].ProductId,
			// ProductName: data[i].ProductName,
			// BusinessId:  data[i].BusinessId,
			// PayUser:     data[i].UserName,
			// PayUserId:   data[i].UserId,
			Id:               data[i].Order.Id,
			OrderId:          data[i].Order.Id,
			ProductId:        data[i].Order.ProductId,
			OrderCode:        data[i].Code,
			DivAmount:        data[i].CompanyAmount,
			DivTime:          data[i].PayTime,
			ProductName:      data[i].Order.ProductName,
			BusinessId:       data[i].Order.BusinessId,
			DivStatus:        data[i].Status,
			PayUserId:        data[i].UserId,
			PayUser:          data[i].UserName,
			DealerId:         data[i].DealerId,
			DealerName:       data[i].Dealer.Name,
			BusinessName:     data[i].Business.Name,
			BusinessUserId:   data[i].Business.UserId,
			BusinessUserName: data[i].BusinessUser.Name,
			InviteId:         data[i].InviteId,
			InviteName:       data[i].InviteUser.Name,
		}
		fmt.Printf("%#v\n", data[i])
	}

	return &types.CapitalRecordRsp{
		List:      list,
		Count:     count,
		SumAmount: amount,
	}, nil
}
